package streamforce.crypto.user;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import streamforce.crypto.event.TransformedCTEventAll;
import streamforce.crypto.event.UserInitAggEvent;
import streamforce.crypto.event.UserInitEvent;
import streamforce.crypto.message.PolicyMessage;
import streamforce.crypto.message.UserHandshakeMessage;
import streamforce.crypto.policies.Policy;
import streamforce.crypto.user.subscriber.UserSelectAllSimple;
import streamforce.crypto.user.subscriber.UserSelectPriceSimple;
import streamforce.crypto.user.subscriber.UserSumAllAgg3;
import streamforce.crypto.user.subscriber.UserSumPriceAgg1;
import streamforce.crypto.user.subscriber.UserSumPriceAgg3;

/**
 * Connecting to the server.
 * 
 * It only has to listen to new messages: initilization and
 * then decryption
 *
 */
public class EPUserClientAgg3 extends EPUserClientSimple{
	private final Log log = LogFactory.getLog(EPUserClientAgg3.class); 
	public EPUserClientAgg3(int ui, int type) {
		super(ui, type);
		int[] ids = {this.userId};
		this.uhm = new UserHandshakeMessage(ids,
				PolicyMessage.AGG_QUERY_3);
	}		
	
	@Override
	protected void initializeUser(UserInitEvent pmu){				
		if (this.type==EPUserClientSimple.SELECT_PRICE)
			this.userPolicy = new UserSumPriceAgg3(this.cryptoHandler);
		else if (this.type == EPUserClientSimple.SELECT_ALL)
			this.userPolicy = new UserSumAllAgg3(this.cryptoHandler);
		this.userPolicy.initUser_simple(pmu.getUserId(),2,pmu.getPubK()[0],
				pmu.gettK()[0], pmu.getDecK()[0], pmu.getDetK()[0]);
		this.userPolicy.initUser_simple(pmu.getOtherUserId(), 2, pmu.getPubK()[0],
				pmu.gettK()[1], pmu.getDecK()[1], pmu.getDetK()[0]);		
					
		log.info("user "+this.userId+" initialized , RID = "+pmu.getOtherUserId()); 
	}
		
	public static void main(String[] args){
		int id = new Integer(args[0]).intValue();
		(new EPUserClientAgg3(id,EPUserClientAgg3.SELECT_ALL)).start();
	}
}
